﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ovvio.Web.Core;
using Ovvio.Data;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Data;
using System.Web.Mvc;

namespace Ovvio.Web.Areas.Admin.Models
{
    [TypeConverter(typeof(ClassCoverter))]
    public class CatalogueModel : BaseModel<Product>
    {
        [ScaffoldColumn(false)]
        public int Id { get { return Entity.Id; } 
            set { 
                Entity.Id = value; } 
        }

        [ScaffoldColumn(false)]
        public string Name { get { return Entity.Name; } set { Entity.Name = value; } }

        [DisplayName("On-Stock")]
        public bool IsOnStock { get { return Entity.IsOnStock; } set { Entity.IsOnStock = value; } }
        [DisplayName("Published")]
        public bool IsPublished { get { return Entity.IsPublished; } set { Entity.IsPublished = value; } }

        public int RatingId
        {
            get { return Entity.ProductRating == null ? -1 : Entity.ProductRating.Id; }

            set { Entity.ProductRatingReference.EntityKey = new EntityKey("OvvioDataContext.ProductRatings", "Id", value); }
        }

        public void SetRatingId(int id)
        {
            Entity.ProductRatingReference.EntityKey = new EntityKey("OvvioDataContext.ProductRatings", "Id", id);
        }

        public void SetItemTypeId(int id)
        {
            Entity.ItemTypeReference.EntityKey = new EntityKey("OvvioDataContext.ItemTypes", "Id", id);
        }

        [UIHint("RatingUserControl")]
        public RatingModel Rating
        {
            get { return Entity.ProductRating == null ? new RatingModel() { Id = -1, Description="None" } : new RatingModel() { Entity = Entity.ProductRating }; }

            set {
                Entity.ProductRating = value.Entity;
                Entity.ProductRatingReference.EntityKey = new EntityKey("OvvioDataContext.ProductRatings", "Id", value.Id); }
        }


        public SelectList Ratings
        {
            get
            {
                return CreateList<ProductRating, OvvioDataContext>("Id", "Description", p => p.Id == RatingId);
            }
        }

        public int PromoId
        {
            get { return Entity.Promo == null ? -1 : Entity.Promo.Id; }
        }


        public int ItemTypeId
        {
            get { return Entity.ItemType == null ? -1 : Entity.ItemType.Id; }

            set { Entity.ItemTypeReference.EntityKey = new EntityKey("OvvioDataContext.ItemTypes", "Id", value); }
        }

     

        [UIHint("ItemTypeUserControl")]
        public ItemTypeModel ItemType
        {
            get { return Entity.ItemType == null ? new ItemTypeModel() { Id = -1, Description = "None" } : new ItemTypeModel() { Entity = Entity.ItemType }; }

            set
            {
                Entity.ItemType = value.Entity;
                Entity.ItemTypeReference.EntityKey = new EntityKey("OvvioDataContext.ItemTypes", "Id", value.Id);
            }
        }

        public SelectList ItemTypes
        {
            get
            {
                return CreateList<ItemType, OvvioDataContext>("Id", "Description", p => p.Id == ItemTypeId);
            }
        }

        [ScaffoldColumn(false)]
        public byte[] Image
        {
            get { return Entity.Image; }
            set { Entity.Image = value; }
        }
    }

    public class ClassCoverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return (sourceType == typeof(string)) ? true : base.CanConvertFrom(context, sourceType);
        }
    }
 


}
